package com.codi.fcloud.ufx.hssale.portfolio;

import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.ExceptionUtil;
import com.codi.bus.exception.HundsunAPIException;
import com.codi.fcloud.ufx.UfxSDKFactory;
import com.codi.fcloud.ufx.constant.HSInputParameterNames;
import com.codi.fcloud.ufx.constant.UrlConstants;
import com.codi.trade.dto.PortfolioTradeDto;
import com.codi.trade.service.portfolio.HsPortfolioTradeService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 投资组合交易
 *
 * @author spy
 * @version 1.0 2017-11-06 10:17
 * @since 1.0
 */
@Slf4j
@Service
public class HsPortfolioTradeServiceImpl extends UfxSDKFactory implements HsPortfolioTradeService {
    @Override
    @Loggable
    public Map<String, Object> makeTrade(PortfolioTradeDto dto) {

        Preconditions.checkNotNull(dto);
        Preconditions.checkNotNull(dto.getTrade_acco());
//        Preconditions.checkNotNull(dto.getTa_acco());  //TODO 这个必传？
        Preconditions.checkNotNull(dto.getPassword());
        Preconditions.checkNotNull(dto.getFund_busin_code());
        Preconditions.checkNotNull(dto.getCapital_mode());
        Preconditions.checkNotNull(dto.getCommunic_content());


        String trustWay = apiValueMap.get(HSInputParameterNames.TRUST_WAY);
        Map<String, Object> request = new HashMap<>();

        request.put("trust_way", trustWay);
        request.put("net_no", getString(dto.getNet_no(), "0001"));


        request.put("trade_acco", getString(dto.getTrade_acco())); //	C17	交易账号	Y
        request.put("ta_acco", getString(dto.getTa_acco()));//	C12	TA账号	Y
        request.put("password", getString(dto.getPassword()));//	C50	密码	Y	传加密后的密文,默认MD5(加密后密文是32位)加密方式
        request.put("apply_time", getString(dto.getApply_time()));//	C6	申请时间	N
        request.put("order_date", getString(dto.getOrder_date())); //	N8	下单日期	N	为空则下单日期取服务器收到报文的请求日期；不为空则该日期为下单日期；

        request.put("fund_busin_code", getString(dto.getFund_busin_code())); //	C3	业务代码	Y	022申购
        request.put("capital_mode", getString(dto.getCapital_mode())); //	C2	资金方式	Y
        request.put("detail_fund_way", getString(dto.getDetail_fund_way()));  //	C2	明细资金方式	N

        //"自定义组合投资格式：基金代码A|A的份额类别|A的投资金额|基金代码B|B的份额类别|B的投资金额|…|基金代码N|N的份额类别|N的投资金额
        //套餐组合投资合格：投资组合编码|投资金额"
        request.put("communic_content", getString(dto.getCommunic_content())); //	C600	通讯内容	Y
//
        request.put("cust_portfolio_code", getString(dto.getCust_portfolio_code())); //	C30	自定义组合编号	N
        request.put("portfolio_code", getString(dto.getPortfolio_code())); //	C4	投资组合编码	N	投资组合编号和自定义编号，必选一个
        request.put("fund_interface_type", getString(dto.getFund_interface_type())); //	C1	接口类型	N
        request.put("protocol_name", getString(dto.getProtocol_name())); //	C40	协议名称	N
        request.put("invest_flag", getString(dto.getInvest_flag())); //	C1	投资标志	Y	0：套餐组合投资；1：自定义组合投资
        request.put("busin_ass_code", getString(dto.getBusin_ass_code())); //	C2	业务辅助代码	N

        request.put("ext_combcode", getString(dto.getExt_combcode()));
        try {
            Map<String, Object> response = getUfxSDK().transact(getTarget(), request);

            return appendLog(request, response);
        } catch (Exception exception) {
            ExceptionUtil.logError(logger, getTarget(), exception);
            throw new HundsunAPIException(ExceptionUtil.getMessage(exception));
        }
    }

    @Override
    protected String getTarget() {
        return UrlConstants.PREFIX_SALE + UrlConstants.PORTFOLIO_TRADE;
    }
}
